# coding: utf-8
import pickle
import random
import threading
from random import shuffle

import numpy as np
import matplotlib.pyplot as plt

from TSP import TSP

np.set_printoptions(suppress=True, linewidth=999, threshold=np.inf)
plt.rc('font', family='Times New Roman', weight='normal', size=12)


def copy(target):
    return pickle.loads(pickle.dumps(target))


class Encoding:
    def __init__(self, data, salesmen: int, min_visit: int):
        self.cities = None
        self.breaks = None  # breakpoints
        self.cost = 1e99
        if data:
            self.cities = copy(tsp.cities)
            shuffle(self.cities)
            # Modify Breaks
            self.breaks = []
            head, tail = 1 + min_visit, len(self.cities) // salesmen + min_visit + 1
            while len(self.breaks) < salesmen - 1:
                self.breaks.append(random.randint(head, tail))
                head = self.breaks[-1] + 1
                tail = len(self.cities) // salesmen * (len(self.breaks) + 1) + min_visit + 1
    
    def __repr__(self):
        tours = []
        breaks = [0] + [i - 1 for i in self.breaks] + [len(self.cities)]
        print(breaks)
        for i in range(1, len(breaks)):
            tour = self.cities[breaks[i - 1]: breaks[i]]
            tours.append(tour)
        return f'cities: {self.cities}, costs: {self.breaks}, total: {self.cost}\n' \
               f'tours: {tours}'


class PGA:
    def __init__(self, data):
        self.data = data


tsp = TSP(path=f'../../data/TSPLIB/burma14.tsp.txt')
encoding = Encoding(tsp, salesmen=3, min_visit=3)
print(encoding)
